home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / font_subs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-27  |  4.2 KB  |  178 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: font_subs.c,v 1.2 89/09/27 08:37:20 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/font_subs.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/font_subs.c,v $$Revision: 1.2 $";
  12.  
  13. /* font routines */
  14.  
  15. #include "bitmap.h"
  16. #include "font.h"
  17. #include "default_font.h"
  18. #include "window.h"
  19. #include <stdio.h>
  20. #ifdef DEBUG
  21. #   include "defs.h"
  22. #endif
  23.  
  24. #define HEAD(x)        font->head.x
  25.  
  26. /**************************************************************************
  27.  *
  28.  *    set up a font file
  29.  */
  30.  
  31. struct font *
  32. open_font(file)
  33. char *file;            /* name of font file */
  34.    {
  35.    FILE *fp;
  36.    int size;
  37.    char *malloc();
  38.    struct font *font, *open_sfont();
  39.  
  40.    if (file == (char *) 0 || *file == '\0') {
  41.       return(open_sfont(default_font_head,&default_font));
  42.       }
  43.  
  44. #ifdef DEBUG
  45.       dprintf(f)(stderr,"Opening font file [%s]\n",file);
  46. #endif
  47.  
  48.    if ((fp=fopen(file,"r"))  == NULL)
  49.       return((struct font *)0);
  50.  
  51.    if ((font=(struct font *) malloc(sizeof(struct font))) == (struct font *)0) {
  52.       fclose(fp);
  53.       return((struct font *)0);
  54.       }
  55.  
  56.    if (fread(&(font->head),HEADER_SIZE,1,fp) != 1) {
  57.       free((char *) font);
  58.       fclose(fp);
  59.       return((struct font *)0);
  60.       }
  61.  
  62.    if (HEAD(type) != FONT_A) {
  63.       free((char *) font);
  64.       fclose(fp);
  65.       return((struct font *)0);
  66.       }
  67.                                
  68.     /* fonts are always 32 bit aligned */
  69.  
  70.     size = (HEAD(wide)*HEAD(count)+31)&~31;
  71.  
  72.    font->data = bit_alloc(size,HEAD(high),NULL_DATA,1);
  73.    font->table = (struct entry **) 0;
  74.  
  75.    /* read in font data */
  76.  
  77.    size = BIT_SIZE(font->data);
  78.    fread(BIT_DATA(font->data), size, 1, fp);
  79.  
  80.    /* create individual characters */
  81.  
  82.    glyph_create( font );
  83.  
  84.    fclose(fp);
  85.    return(font);
  86.    }
  87.  
  88.  
  89. /**************************************************************************
  90.  *
  91.  *    deallocate a font
  92.  */
  93.  
  94. int
  95. free_font(dead_font)
  96. struct font *dead_font;        /* font to be deallocated */
  97.    {
  98.    register int i;
  99.  
  100.    if (!dead_font)
  101.       return(-1);
  102.  
  103.    for(i=0;i<MAXGLYPHS;i++)
  104.       if (dead_font->glyph[i])
  105.          bit_destroy(dead_font->glyph[i]);
  106.    if (dead_font->head.type != FONT_S)
  107.       bit_destroy(dead_font->data);
  108.    zap_fhash(dead_font);        /* free up hash table space */
  109.    i=font_purge(dead_font);    /* eliminate references to dead font */
  110.  
  111. #ifdef DEBUG
  112.       dprintf(f)(stderr,"freeing font %d (%d references)\n",
  113.       dead_font->ident,i);
  114. #endif
  115.  
  116.    free((char *) dead_font);
  117.    }
  118.  
  119. /**************************************************************************
  120.  *
  121.  *    set up a static font file
  122.  */
  123.  
  124. struct font *
  125. open_sfont(head,data)
  126. struct font_header head;    /* font header */
  127. BITMAP *data;        /* array of bits */
  128.    {
  129.    char *malloc();
  130.    struct font *font;
  131.  
  132.    if ((font=(struct font *) malloc(sizeof(struct font))) == (struct font *)0)
  133.       return((struct font *)0);
  134.  
  135.    font->head = head;
  136.    font->data = data;
  137.    font->head.type = FONT_S;
  138.    font->table = (struct entry **) 0;
  139.  
  140.    /* create individual characters */
  141.  
  142.    glyph_create( font );
  143.  
  144.    return(font);
  145.    }
  146.  
  147.  
  148. static
  149. glyph_create( font )
  150. struct font *font;
  151.    {
  152.    register int i, x;
  153.    int        first = HEAD(start);
  154.    int        last = HEAD(start) + HEAD(count);
  155.    int        wide = HEAD(wide);
  156.    int        high = HEAD(high);
  157.    int        nochar;
  158.    
  159.    /* Pick the character to be printed for characters not in the set.
  160.       Normally, it is the character specified by C_NOCHAR, but it that isn't
  161.       in the range of the set, we pick the first character (which is usually
  162.       a space).
  163.    */
  164.    nochar = C_NOCHAR - HEAD(start);
  165.    if( nochar >= last )
  166.     nochar = 0;
  167.    nochar *= wide;
  168.  
  169.    x = 0;
  170.    for(i=0; i<MAXGLYPHS; i++)
  171.       if (i < first || i >= last)
  172.          font->glyph[i] = bit_create(font->data, nochar, 0, wide, high);
  173.       else {
  174.          font->glyph[i] = bit_create(font->data, x, 0, wide, high);
  175.          x += wide;
  176.          }
  177.    }
  178.